/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package upc.jfactory.scp.jpa.controlador;

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import upc.jfactory.scp.jpa.bean.OfertaLaboral;
import upc.jfactory.scp.jpa.controlador.exceptions.NonexistentEntityException;
import upc.jfactory.scp.jpa.controlador.exceptions.PreexistingEntityException;

/**
 *
 * @author Diego
 */
public class OfertaLaboralJpaController {

    public OfertaLaboralJpaController() {
        emf = Persistence.createEntityManagerFactory("Web-SCPPU");
   
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public OfertaLaboral createOfertaLaboral(OfertaLaboral oferta) {
           EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            em.persist(oferta);
            em.getTransaction().commit();
        } catch (Exception ex) {
          
       
        } finally {
            if (em != null) {
                em.close();
             
            }
               return oferta;
        }
  }
 public void removeOfertaLaboral(int id) {
            EntityManager em = getEntityManager();
    OfertaLaboral emp = findOfertaLaboral(id);
    if (emp != null) {
      em.remove(emp);
      em.close();
    }
  }
 public OfertaLaboral crear(OfertaLaboral ofertaLaboral) throws PreexistingEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            em.persist(ofertaLaboral);
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findOfertaLaboral(ofertaLaboral.getOfertalaboralId()) != null) {
                throw new PreexistingEntityException("OfertaLaboral " + ofertaLaboral + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
            return findOfertaLaboral(ofertaLaboral.getOfertalaboralId());
        }
    }
    public void create(OfertaLaboral ofertaLaboral) throws PreexistingEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            em.persist(ofertaLaboral);
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findOfertaLaboral(ofertaLaboral.getOfertalaboralId()) != null) {
                throw new PreexistingEntityException("OfertaLaboral " + ofertaLaboral + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(OfertaLaboral ofertaLaboral) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            ofertaLaboral = em.merge(ofertaLaboral);
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = ofertaLaboral.getOfertalaboralId();
                if (findOfertaLaboral(id) == null) {
                    throw new NonexistentEntityException("The ofertaLaboral with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            OfertaLaboral ofertaLaboral;
            try {
                ofertaLaboral = em.getReference(OfertaLaboral.class, id);
                ofertaLaboral.getOfertalaboralId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The ofertaLaboral with id " + id + " no longer exists.", enfe);
            }
            em.remove(ofertaLaboral);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<OfertaLaboral> findOfertaLaboralEntities() {
        return findOfertaLaboralEntities(true, -1, -1);
    }

    public List<OfertaLaboral> DameOfertaLaboraxEmpresa(int idempresa){
    EntityManager em = getEntityManager();
         try
        {
              Query q = em.createNamedQuery("OfertaLaboral.findByEmpresaId");
              q.setParameter("empresaId",idempresa);
              return q.getResultList();
        }
        finally
        {
            em.close();
        }
    }

    public List<OfertaLaboral> findOfertaLaboralEntities(int maxResults, int firstResult) {
        return findOfertaLaboralEntities(false, maxResults, firstResult);
    }

    private List<OfertaLaboral> findOfertaLaboralEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select object(o) from OfertaLaboral as o");
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public OfertaLaboral findOfertaLaboral(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(OfertaLaboral.class, id);
        } finally {
            em.close();
        }
    }

    public int getOfertaLaboralCount() {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select count(o) from OfertaLaboral as o");
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }


}
